﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using ProjTemp.Desktop.Common.Controls;
using ProjTemp.Desktop.Common.View;
using System.Windows.Data;

namespace ProjTemp.Desktop.Common.Container
{
    public class WindowWrapper : IContainer
    {
        private readonly CustomPopupWindow _window;
        private object _windowTitle;

        /// <summary>
        /// Initializes a new instance of <see cref="WindowWrapper"/>.
        /// </summary>
        public WindowWrapper()
        {
            this._window = new CustomPopupWindow();
            this._windowTitle = string.Empty;
        }

        /// <summary>
        /// Ocurrs when the <see cref="Window"/> is closed.
        /// </summary>
        public event EventHandler Closed
        {
            add { this._window.Closed += value; }
            remove { this._window.Closed -= value; }
        }

        /// <summary>
        /// Gets or Sets the content for the <see cref="Window"/>.
        /// </summary>
        public object Content
        {
            get { return this._window.Content; }
            set { this._window.Content = value; }
        }

        /// <summary>
        /// Gets or Sets the <see cref="Window.Owner"/> control of the <see cref="Window"/>.
        /// </summary>
        public object Owner
        {
            get { return this._window.Owner; }
            set { this._window.Owner = value as Window; }
        }

        /// <summary>
        /// Closes the <see cref="Window"/>.
        /// </summary>
        public void Close()
        {
            this._window.Close();
        }

        public object Title
        {
            get { return this._windowTitle; }
            set 
            {
                this._windowTitle = string.Empty;
                if(value != null) this._windowTitle = value; 
            }
        }

        public void Show(ContainerSetting setting = null)
        {
            if(setting != null)
                this.ApplySetting(setting);
            this.SetWindowTitle();
            this._window.ShowDialog();
        }

        public void ShowNonModal(ContainerSetting setting = null)
        {
            if(setting != null)
                this.ApplySetting(setting);
            this.SetWindowTitle();
            this._window.Show();
        }

        private object _returnValue;
        public object ReturnValue
        {
            get { return this._returnValue; }
            set { this._returnValue = value; }
        }

        private void ApplySetting(ContainerSetting setting)
        {
            if (setting == null)
                throw new ArgumentNullException("setting");

            if (!double.IsNaN(setting.Height))
                this._window.Height = setting.Height;
            if (!double.IsNaN(setting.Width))
                this._window.Width = setting.Width;
            this._window.ShowInTaskbar = setting.ShowInTaskBar;
            this._window.WindowState = setting.WindowState;
            this._window.ShowMinButton = setting.ShowMinButton;
            this._window.ShowMaxRestoreButton = setting.ShowMaxButton;
            this._window.ShowCloseButton = setting.ShowCloseButton;
            this._window.WindowStartupLocation = setting.WindowStartupLocation;
            if (setting.WindowStartupLocation == WindowStartupLocation.CenterOwner && this.Owner == null)
                this._window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            this._window.ResizeMode = setting.ResizeMode;
        }

        private void SetWindowTitle()
        {
            this._window.Title = this._windowTitle.ToString();
            var view = this.Content as VBase;
            if (view != null)
            {
                Binding headerBinding = new Binding(DesktopConst.ViewHeaderPropertyName);
                headerBinding.Source = view;
                headerBinding.Mode = BindingMode.TwoWay;
                this._window.SetBinding(Window.TitleProperty, headerBinding);
            }
        }
    }
}
